//
//  SearchController.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/3.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class SearchController: BaseViewController {
    
    lazy var textField : UITextField = {
    
    
        let field = UITextField(frame: CGRect(x: 30, y: 0, width: KScreenW - 100, height: 32))
        
        field.placeholder = "输入你要找的宝贝"
        
        field.font = UIFont.systemFont(ofSize: 14)
        
        
        field.tintColor = UIColor.themeColor()
        
        field.clearButtonMode = .whileEditing
        
        field.returnKeyType = .search
        
        field.delegate = self

    
        return field
    
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setNav()

        // Do any additional setup after loading the view.
    }
    
    
    
    
    func setNav() {
        
        let bgView = NavTittleView(frame: CGRect(x: 0, y: 0, width: KScreenW, height: 44))
        
        bgView.backgroundColor = UIColor.clear
        
        let btn = LightDisableBtn(type: .custom)
        
        btn.frame = CGRect(x: 20, y: 6, width: KScreenW - 70, height: 32)
        
        btn.setBackgroundImage(UIImage(named: "bg_search_bar_7_gray"), for: .normal)
        
        btn.backgroundColor = UIColor.clear
        
        bgView.addSubview(btn)
        
        
        
        
        btn.addSubview(textField)
        
//        label.textColor = UIColor.colorWith(r: 145, g: 145, b: 145)

        
        let rightBtn = LightDisableBtn(type: .custom)
        
        rightBtn.frame = CGRect(x: KScreenW - 50, y: 0, width: 50, height: 44)
        
        rightBtn.setTitle("取消", for: .normal)
        rightBtn.setTitle("取消", for: .highlighted)
        rightBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)

        
        rightBtn.backgroundColor = UIColor.clear
        
        rightBtn.addTarget(self, action: #selector(rightBtnDidTap), for: .touchUpInside)
        
        bgView.addSubview(rightBtn)
        
        navigationItem.titleView = bgView
        
    }
    

    override func viewWillAppear(_ animated: Bool) {
        
        super.viewWillAppear(animated)
        
        textField.text = nil
        
        textField.becomeFirstResponder()

    }
    
    
    
    func rightBtnDidTap(){
        
        textField.resignFirstResponder()
        
        dismiss(animated: true, completion: nil)
    }

    


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
 
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}



extension SearchController : UITextFieldDelegate{



    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        
        if textField.isFirstResponder {
            
            
            textField.resignFirstResponder()
        }
        
        QL1("点击了")
        
        navigationController?.pushViewController(BaseViewController(), animated: true)
        
        
        return true

    }






}
